;
(function() {
  new Vue({
    el: '#business-config',
    data: {
      channelTypeList: [],
      channelNameList: [],
      allChannelNameList: [],
      isShowChannelTypeIpt: false,
      isShowChannelNameIpt: false,
      addTypeValue: null,
      addNameValue: null,
      parentCode: null,
      tmpName: null // 用于编辑时缓存名称
    },
    directives: {
      focus: {
        inserted: function(el) {
          el.focus()
        },
        update: function(el) {
          el.focus()
        }
      }
    },
    methods: {
      getChannelType() {
        var self = this;
        hlhttp.get({
          url: 'configure/dict',
          data: {
            'module_code': 'SCTZ',
            'config_name': 'SCTZ_CHANNEL_CATE'
          },
          success: data => {
            data.forEach(el => {
              el['isEdit'] = 'N'
              el['isSelect'] = 'N'
            })
            self.channelTypeList = data;
          }
        })
      },
      getChannelName() {
        var self = this;
        hlhttp.get({
          url: 'configure/dict',
          data: {
            'module_code': 'SCTZ',
            'config_name': 'SCTZ_CHANNEL_NAME'
          },
          success: data => {
            data.forEach(el => el['isEdit'] = 'N')
            self.allChannelNameList = data;
          }
        })
      },
      selectChannelType(name, code, i) {
        this.parentCode = code;
        this.channelNameList = this.allChannelNameList.filter(el => el.parentDictCode == code);
        this.channelTypeList.forEach(el => el['isSelect'] = 'N')
        this.$set(this.channelTypeList[i], 'isSelect', 'Y')
      },
      addType() {
        this.addTypeValue = null;
        this.isShowChannelTypeIpt = true;
      },
      addChannel() {
        this.addNameValue = null;
        this.isShowChannelNameIpt = true;
      },
      editChannelType(i) {
        this.tmpName = this.channelTypeList[i]['dictName']
        this.$set(this.channelTypeList[i], 'isEdit', 'Y')
      },
      delChannelType(code, i) {
        var self = this;
        hlhttp.delete({
          url: 'configure/merchant_channel?module_code=SCTZ&&config_name=SCTZ_CHANNEL_CATE&&dict_code=' + code,
          success: data => {
            self.channelTypeList.splice(i, 1);
            hl.toast('删除成功')
          }
        })
      },
      editChannelName(i) {
        this.$set(this.channelNameList[i], 'isEdit', 'Y')
      },
      delChannelName(code, i) {
        var self = this;
        hlhttp.delete({
          url: 'configure/merchant_channel?module_code=SCTZ&&config_name=SCTZ_CHANNEL_NAME&&dict_code=' + code,
          success: data => {
            self.getChannelName()
            self.channelNameList.splice(i, 1);
            hl.toast('删除成功')
          }
        })
      },
      hideChannelTypeIpt() {
        this.isShowChannelTypeIpt = false;
      },
      hideChannelNameIpt() {
        this.isShowChannelNameIpt = false;
      },
      confirmAddChannelType() {
        var self = this;
        if (!self.addTypeValue) {
          hl.toast('渠道分类不能为空');
          return
        }

        hlhttp.post({
          url: 'configure/merchant_channel',
          data: {
            'module_code': 'SCTZ',
            'config_name': 'SCTZ_CHANNEL_CATE',
            'parent_code': '#',
            'value': self.addTypeValue
          },
          success: data => {
            self.channelTypeList.push({
              'dictCode': data.dictCode,
              'dictName': self.addTypeValue,
              'isEdit': 'N',
              'isSelect': 'N'
            })
            this.selectChannelType(self.addTypeValue, data.dictCode, self.channelTypeList.length - 1)
            self.isShowChannelTypeIpt = false;
            hl.toast('新增成功')
          }
        })
      },
      cancelAddChannelType() {
        this.isShowChannelTypeIpt = false;
      },
      confirmAddChannelName() {
        var self = this;
        if (!self.addNameValue) {
          hl.toast('渠道名称不能为空');
          return
        }

        hlhttp.post({
          url: 'configure/merchant_channel',
          data: {
            'module_code': 'SCTZ',
            'config_name': 'SCTZ_CHANNEL_NAME',
            'parent_code': self.parentCode,
            'value': self.addNameValue
          },
          success: data => {
            self.getChannelName()
            self.channelNameList.push({
              'dictCode': data.dictCode,
              'dictName': self.addNameValue,
              'parentDictCode': self.parentCode,
              'isEdit': 'N'
            })
            self.isShowChannelNameIpt = false;
            hl.toast('新增成功')
          }
        })
      },
      cancelAddChannelName() {
        this.isShowChannelNameIpt = false;
      },
      confirmEditChannelType(name, code, i) {
        var self = this;
        hlhttp.put({
          url: 'configure/merchant_channel',
          data: {
            'module_code': 'SCTZ',
            'config_name': 'SCTZ_CHANNEL_CATE',
            'dict_code': code,
            'value': name
          },
          success: data => {
            self.$set(this.channelTypeList[i], 'isEdit', 'N')
            hl.toast('编辑成功')
          }
        })
      },
      cancelEditChannelType(i) {
        this.$set(this.channelTypeList[i], 'dictName', this.tmpName)
        this.$set(this.channelTypeList[i], 'isEdit', 'N')
      },
      confirmEditChannelName(name, code, i) {
        var self = this;
        hlhttp.put({
          url: 'configure/merchant_channel',
          data: {
            'module_code': 'SCTZ',
            'config_name': 'SCTZ_CHANNEL_NAME',
            'dict_code': code,
            'value': name
          },
          success: data => {
            self.$set(self.channelNameList[i], 'isEdit', 'N')
            hl.toast('编辑成功')
          }
        })
      },
      cancelEditChannelName(i) {
        this.$set(this.channelNameList[i], 'isEdit', 'N')
      }
    },
    created() {
      this.getChannelType()
      this.getChannelName()
    },
    mounted() {

    }
  })

}())
